package com.yc.homepage.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

/**
 *
 * <h1>在过滤器中存储客户端发起请求的时间戳</>
 * @author yc112
 * Component: 标注是spingBoot bean
 */
@Component
public class PrevRequestFilter extends ZuulFilter {

    @Override
    public String filterType() {
        // 发起请求之前，表示请求最开始的时候
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        // 顺序， 数字越小优先级越高
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        // 是否启用当前过滤器
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        // 用于在过滤器之间传递消息，继承自ConcurrentHashMap
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.set("startTime", System.currentTimeMillis());

        return null;
    }
}
